import 'package:flutter/material.dart';
import 'package:news/com.comm/widget/translate_global_set_item.dart';

import '../contants/constant.dart';
import '../model/db_utils.dart';
import '../utils/utils.dart';
import '../utils/widget_utils.dart';

/**
 * 全局设置弹窗
 */
class TranslateGlobalSet extends StatefulWidget {
  TranslateGlobalSet({
    Key? key,
  }) : super(key: key);

  @override
  _TranslateGlobalSetState createState() => _TranslateGlobalSetState();
}

class _TranslateGlobalSetState extends State<TranslateGlobalSet> {
  List transMap = []; // 要预览的多语言数据
  final db = DbUtils();

  @override
  void initState() {
    super.initState();
    init();
  }

  Future<void> init() async {
    List list = await db.loadTranslateConfig();
    if (list.isNotEmpty) {
      setState(() {
        transMap = list;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: 16,
        ),
        Container(
            width: 1000,
            child: Row(mainAxisSize: MainAxisSize.max, children: [
              _getTitle("翻译语种"),
              _getTitle("语种简称"),
              _getTitle("values后缀"),
              _getTitle("API翻译简称"),
              _getTitle("故障码简称"),
              _getTitle("是否翻译")
            ])),
        SizedBox(
            width: 1000,
            height: 390,
            child: ListView.builder(
                itemCount: transMap.length,
                itemBuilder: (BuildContext context, int index) {
                  return SizedBox(
                      height: 60,
                      child: TranslateGlobalSetItem(
                          data: transMap[index],
                          backIndex: index,
                          backData: ((changeData, backIndex) {
                            transMap[backIndex] = changeData;
                          })));
                })),
        Row(children: <Widget>[
          Expanded(
              child: SizedBox(
            height: 1,
          )),
          TextButton(
            child: Text("保存"),
            onPressed: () async {
              await db.saveTranslateConfig(transMap);
              showToast("多语言配置保存成功！");
              Navigator.pop(context);
            },
          ),
          SizedBox(
            width: 16,
          ),
          TextButton(
            child: Text("新增"),
            onPressed: () async {
              Map<dynamic, dynamic> newItem = {};
              setState(() {
                transMap.add(newItem);
              });
            },
          ),
          SizedBox(
            width: 16,
          ),
          TextButton(
            child: Text("重置"),
            onPressed: () async {
              List mapList =
                  Constant.translate_list.map((bean) => bean.toJson()).toList();
              await db.saveTranslateConfig(mapList);
              showToast("多语言配置重置成功！");
              setState(() {
                transMap = mapList;
              });
            },
          ),
          SizedBox(
            width: 16,
          ),
          TextButton(
            child: Text("取消"),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ]),
      ],
    );
  }

  ///单个标题
  Widget _getTitle(String title) {
    return Flexible(
        child: Container(
            child: getTextWidget(16, title, Colors.black),
            alignment: Alignment.center),
        flex: 1,
        fit: FlexFit.tight);
  }
}
